package com.canbot.u05.activity

import android.app.Activity
import android.app.Dialog
import android.opengl.Visibility
import android.os.Bundle
import android.util.Log
import android.view.View

import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.utils.OkHttpUtils
import com.canbot.u05.utils.ToastTools
import com.canbot.u05.view.KeyboardLayout
import com.canbot.u05.view.PasswordLayout
import com.canbot.u05.widget.LoadingDialogUtils
import kotlinx.android.synthetic.main.activity_password.*
import kotlinx.android.synthetic.main.layout_num_keyboard.*

import java.util.HashMap

class PassWordActivity : BaseActivityNoTitle(), PasswordLayout.OnPasswordChangeListener {
    private var loadingDialog: Dialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_password)
        //绑定键盘
        password_container.setKeyBoard(keyboard_container)
        password_container.setOnPasswordChangeLisener(this)
    }

    /**
     * 点击确定按钮回调
     * @param password 加密后密码
     */
    override fun onComplete(password: String) {
        Log.e(TAG, "密码输入完成  $password")
        verifyPassword(password)
    }

    /**
     * 正在输入中回调
     */
    override fun inputting(){
        if (tv_wrong_password.isShown){
            tv_wrong_password.visibility = View.GONE
        }
    }

    /**
     * 密码校验
     */
    private fun verifyPassword(password: String) {
        loadingDialog = LoadingDialogUtils.createLoadingDialog(this, resources.getString(R.string.request))
        val hasMap = HashMap<String, Any>()
        hasMap["adminType"] = "queryPassword"
        hasMap["queryPassword"] = password
        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "admin", hasMap, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                val strMsgBean = JSON.parseObject(result, JsonResponse::class.java)
                if (strMsgBean.rc == 0) {
                    showToast(getString(R.string.password_correct))
                    runOnUiThread {
                        cancelDialog()
                        exit(true)
                    }
                } else {    //密码错误
                    runOnUiThread {
                        tv_wrong_password.visibility = View.VISIBLE
                        cancelDialog()
                        password_container.clear()//清除密码状态
                    }

                }
            }

            override fun onFailure() {
                showToast(getString(R.string.service_error_hint))
                runOnUiThread {
                    cancelDialog()
                    password_container.clear()
                }
            }
        })
    }

    /**
     * 取消加载的Dialog
     */
    private fun cancelDialog() {
        if (loadingDialog != null && loadingDialog!!.isShowing) {
            loadingDialog!!.cancel()
        }
    }


    fun back(view: View) {
        exit(false)
    }

    /**
     * 返回密码验证结果
     * @param isCorrect 是否正确
     */
    private fun exit(isCorrect: Boolean) {
        if (isCorrect) {
            setResult(Activity.RESULT_OK)
        } else {
            setResult(Activity.RESULT_CANCELED)
        }

        finish()
    }

    override fun onBackPressed() {
        exit(false)
    }

    override fun onPause() {
        super.onPause()
        cancelDialog()
        password_container.clear()
    }

    companion object {
        private const val TAG = "PassWordActivity"
    }

}
